﻿using System.IO;
using System.Runtime.Serialization.Json;
using System.Text;

namespace CompanyHub.Helpers
{
    public static class JsonHelper
    {
        public static T Deserialize<T>(string json)
        {
            byte[] _Bytes = Encoding.Unicode.GetBytes(json);
            using (var _Stream = new MemoryStream(_Bytes))
            {
                var _Serializer = new DataContractJsonSerializer(typeof (T));
                return (T) _Serializer.ReadObject(_Stream);
            }
        }


        public static string Serialize(object instance)
        {
            using (var _Stream = new MemoryStream())
            {
                var _Serializer = new DataContractJsonSerializer(instance.GetType());
                _Serializer.WriteObject(_Stream, instance);
                _Stream.Position = 0;
                using (var _Reader = new StreamReader(_Stream))
                {
                    return _Reader.ReadToEnd();
                }
            }
        }
    }
}